﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Streambolics.Hp41s
{
    public class KeyboardMapping
    {
        private Dictionary<int, InstructionFactory> _Dictionary = new Dictionary<int,InstructionFactory>();
        private KeyboardMapping _Parent;

        public KeyboardMapping (KeyboardMapping aParent)
        {
            _Parent = aParent;
        }

        protected KeyboardMapping (bool aShifted)
            : this (aShifted ? StandardKeyboards.Shifted : StandardKeyboards.Plain)

        {
        }
        public KeyboardMapping ()
            : this (null)
        {
        }

        
        public void Assign (int aKey, InstructionFactory aInstruction)
        {
            if (_Dictionary.ContainsKey (aKey))
            {
                _Dictionary.Remove (aKey);
            }
            _Dictionary.Add (aKey, aInstruction);
        }

        public virtual InstructionFactory Assigned (int aKey, Context aContext)
        {
            if (_Dictionary.ContainsKey (aKey))
            {
                return _Dictionary[aKey];
            }
            if (_Parent != null)
            {
                return _Parent.Assigned (aKey, aContext);
            }
            return CommonInstructions.Null;
        }
    }
}
